<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="客户编码:">
        <el-input v-model="queryParams.customerCode" size="small" placeholder="请输入客户编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户全称:">
        <el-input v-model="queryParams.amount" size="small" placeholder="请输入客户全称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="对账金额:">
        <el-input v-model="queryParams.customerName" size="small" placeholder="请输入对账金额" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="业务单据号:">
        <el-input v-model="queryParams.basicNo" size="small" placeholder="请输入业务单据号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="支付流水号:">
        <el-input v-model="queryParams.slipNo" size="small" placeholder="请输入支付流水号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button  type="primary" icon="el-icon-check" size="mini" @click="">对账通过</el-button>
        <el-button  type="danger" icon="el-icon-check" size="mini" @click="">对账不通过</el-button>
        <span style="padding-left: 2px"><el-tag type="info">金额总合：{{ amountSum }}</el-tag></span>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-row :gutter="24" class="form-item-group">
        <el-col :span="12">
          <el-table v-loading="loading"
          :data="page.leftRecords"
          highlight-current-row
          @selection-change="handleSelectionChange"
          @cell-dblclick="dblclick"
          ref="leftTable">
            <!-- <template v-for='(col) in dynamicTableCol'> -->
              <el-table-column type="selection" width="55" align="center" />
              <template v-for='(col) in dynamicTableCol'>
                <el-table-column :label="col.chrName1" :prop="col.chrCode1" :key="col.chrCode1" :align="col.align" />
              </template>
            <!-- </template> -->
          </el-table>
          <!-- 分页 -->
          <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />
        </el-col>
        <el-col :span="12">
            <el-table v-loading="loading"
            :data="page.records"
            highlight-current-row
            @selection-change="handleRightSelectionChange"
            ref="rightTable">
              <el-table-column type="selection" width="55" align="center" />
              <template v-for='(col) in dynamicTableCol'>
                <el-table-column :label="col.chrName1" :prop="col.chrCode1" :key="col.chrCode1" :align="col.align" />
              </template>
            </el-table>
            <!-- 分页 -->
            <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />
        </el-col>
    </el-row>

  </div>
</template>

<script>
import ArConfirmApi from '@/api/accounting/ArConfirmApi'
import AcctCheckConfigApi from '@/api/accounting/AcctCheckConfigApi'
import GenTableApi from '@/api/tool/GenTableApi'
import extendComponents from "@/components/Accounting/extend/data.js";

const defaultArConfirmFormData = {
  customerCode: null,
  amount: null,
  customerName: null,
  adjustAmount: null,
  basicNo: null,
  slipNo: null,
  unionName: null,
  consigneeName: null,
  consigneeAddress: null,
  consgneePhone: null,
  orderSource: null,
  wtCnt: null,
  supplierNo: null,
  supplierName: null,
  orderStatus: '0',
  customClearanceDate: null,
  processPersonName: null,
  paymentDate: null
}
export default {
  name: 'ArConfirmList',
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        customerCode: null,
        amount: null,
        customerName: null,
        adjustAmount: null,
        basicNo: null,
        slipNo: null,
        unionName: null,
        consigneeName: null,
        consigneeAddress: null,
        consgneePhone: null,
        orderSource: null,
        wtCnt: null,
        supplierNo: null,
        supplierName: null,
        orderStatus: null,
        customClearanceDate: null,
        processPersonName: null,
        paymentDate: null,
        isValid: this.$constant.Y
      },
      dynamicTableCol:[],
      acctCheckConfigFormData:[],
      tableColumnOptions:[],
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        leftRecords:[],
        records: []
      },
      arConfirmFormDialog: {
        title: '',
        visible: false
      },
      arConfirmFormData: Object.assign({}, defaultArConfirmFormData),
      arConfirmFormRules: {
        customerCode: [{ required: true, message: '客户编码不能为空', trigger: 'blur' }],
        customerName: [{ required: true, message: '对账金额不能为空', trigger: 'blur' }]
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true,
      amountSum:null
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.openArConfirmFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return ArConfirmApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openArConfirmFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      ArConfirmApi.leftPage(this.queryParams, this.page).then(response => {
        console.log("应收");
        console.log(response);

        this.page.leftRecords = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })

      ArConfirmApi.rPage(this.queryParams, this.page).then(response => {
        console.log("应付");
        console.log(response);

        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })

      AcctCheckConfigApi.get('1624581146954305536').then(response => {
        console.log(response);
        this.acctCheckConfigFormData = {
          ...response.data
        }
        this.getTableColumnList()
      })
    },
    handleRightSelectionChange(selection) {
      //右边表格
      let isChek = false;
      //this.multipleSelection = selection
      console.log(selection)
      if(selection != ''){
        selection.forEach(item =>{
          this.page.leftRecords.forEach(item1 =>{
            if(item.basicNo == item1.basicNo){
                //this.amountSum = this.amountSum+item1.amount
                isChek = true;
                this.$refs['leftTable'].toggleRowSelection(item1, isChek)
            }
          })
        })

        isChek = false;
      }else{

        this.amountSum = null
        this.$refs['leftTable'].clearSelection()
      }

    },
    handleSelectionChange(selection){
      //左边表格
      let isChek = false;
      //this.multipleSelection = selection
      if(selection != ''){
        selection.forEach(item =>{
          this.page.records.forEach(item1 =>{
            if(item.basicNo == item1.basicNo){
                this.amountSum = this.amountSum+item.amount
                isChek = true;
                this.$refs['rightTable'].toggleRowSelection(item1, isChek)
            }
          })
        })

        isChek = false;
      }else{

        this.amountSum = null
        this.$refs['rightTable'].clearSelection()
      }

    },
    getTableColumnList() {
      if (this.acctCheckConfigFormData.configType) {
        GenTableApi.getTableColumnList(this.acctCheckConfigFormData.configType).then(response => {
          this.tableColumnOptions = response.data

          if (this.acctCheckConfigFormData.fields) {
            var fieldList = this.acctCheckConfigFormData.fields.split(',')
          }

          fieldList.forEach(item =>{
            let str = '';
            let fixed = '';
            let str2 = '';
            response.data.forEach(item1 =>{
                if(item1.columnName == 'basic_no'){
                  fixed = 'left'
                }

                if(item1.columnName == item){
                  str = item1.columnComment
                  str2 = item1.javaField
                }
            })
            this.dynamicTableCol.push({
              chrName1:str,
              chrCode1:str2,
              align:'center',
              fixed:fixed
            })
          })
        })
      }
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    },
    dblclick(row, column, cell, event) {
      if(column.property != 'amount'){
        return;
      }
      this.$refs['leftTable'].toggleRowSelection(row, true)
      let cellValue = row[column.property];
      new extendComponents.inputC({
            propsData: {
              // 使用propsData对象传递参数，子组件在props中可以接收到
              cellValue: cellValue, // 传递单元格的值
              saveRowData: this.saveRowData, // 传递回调函数用于保存行数据，组件中可以触发之
              cellDom: cell, // 传递这个dom元素
              row: row, // 传递双击的行的数据
              property: column.property, // 传递双击的是哪个字段
            },
          }).$mount(cell.children[0]); // 5. $mount方法，用于将某个dom挂载到某个dom上

    },
    saveRowData(params) {
        console.log("继承的子组件传递过来的数据", params);
        /**
         * 方式一：使用官方推荐的$mount去挂载到某个节点上，上方也是
         * */
        new extendComponents.spanC({
          propsData: {
            cellValue: params.cellValue,
          },
        }).$mount(params.cellDom.children[0]);
    }

  }
}
</script>
